﻿using Gp.Scripts.Core;
using Helpers;
using Pathfinding;
using Shapes;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Gp.Scripts.GUI
{
    public class MoveAndAttackIndicator : BattleIndicatorBase
    {
        private Polyline _pathLine;
        private Transform _destinationMark;
        private Transform _container;

        

        protected override void OnInit()
        {
            _destinationMark = Root.Find("Container/DestinationMark");
            _pathLine        = Root.Find("Container/PathLine").GetComponent<Polyline>();
            _unitLayer       = LayerMask.GetMask("BattleCharacter");
        }


        protected override void OnDisplay(ShowIndicatorArgs args)
        {
            // 显示
            UI3DNode.ShowSkillRange(args.Unit.ComSkill.WeaponAttack.CalSkillRange());
        }


        protected override void OnCleanUp()
        {
            UI3DNode.CleanUpSkillRange();
        }


        private int _unitLayer;


        private VBattleCell _cachedInfo;

        public override void OnUpdate()
        {
            var currentUnit = Context.Unit;

            // 单位可能已死亡。
            if (currentUnit == null) return;
            // 去掉 Unit 物理层
            if (!Global.Get<CursorManager>().Raycast(out var cursorHit, ~(_unitLayer)))
            {
                return;
            }

            var  sourcePos = currentUnit.transform.position;
            bool isAtkMode = Mouse.current.rightButton.isPressed;


            // if (!NavHelper.SamplePosition(cursorHit.point, NNConstraint.Default, out NNInfo info))
            // {
            //     return;
            // }
            
            var pos = Global.Get<BattleSystem>().CellGrid.GetNearest(cursorHit.point);

            if (_cachedInfo.Node != pos.Node) UpdateLine(sourcePos, pos);
            _cachedInfo = pos;

            if (!Global.Get<CursorManager>().IsMouseButtonDown())
            {
                return;
            }
            // 只有在这个状态中才会显示移动指示器
            // Context.Get<GameState>("GameState")
            // currentUnit.ComSkill.MoveSkill.HumanExecute(new SkillContext { ["Destination"] = destinationPoint });
            Global.Get<BattleSystem>().BattleSettle.GameState.OnVCellSelectEvent(pos, SelectEventType.SELECT);


            // // 鼠标左键按下后的触发逻辑
            // if (!isAtkMode)
            // {
            // }

            // 移动技能触发
            // CleanUp();
        }

        // private ABPath _path;
        private void UpdateLine(Vector3 source, VBattleCell destination)
        {
            // 更新line --> 

            var path = Global.Get<BattleSystem>().CellGrid.ConstructPath(Context.Unit, destination);
            path.BlockUntilCalculated();

            // _pathLine.AddPoint(source);
            _pathLine.points.Clear();
            _pathLine.AddPoint(source);
            foreach (var point in path.VectorPath)
            {
                _pathLine.AddPoint(point);
            }

            _pathLine.UpdateMesh();

            _destinationMark.gameObject.SetActive(true);
            _destinationMark.transform.position = destination.Position;
        }
    }
}